package com.zsj.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsj.entity.OrderResponse;
import com.zsj.entity.ProductQueryRequest;
import com.zsj.entity.ProductQueryResponse;
import com.zsj.entity.WebResponse;
import com.zsj.enums.ErrorCodeEnum;
import com.zsj.exception.BusinessException;
import com.zsj.mysql.entity.RechargeStatics;
import com.zsj.mysql.service.IRechargeStaticsService;
import com.zsj.service.ConsoleService;
import com.zsj.service.MobileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:
 * @Author:
 * @Date:
 */
@RestController
@RequestMapping("/console")
@Slf4j
public class ConsoleController {

    @Autowired
    private ConsoleService consoleService;

    @Autowired
    private MobileService mobileService;

    @Autowired
    private IRechargeStaticsService rechargeStaticsService;

    @PostMapping(value = "/findOrder")
    public WebResponse findOrder(@RequestParam(value = "chargeNo") String chargeNo,
                                 @RequestParam(value = "month", required = false) String month) {
        log.info("订单查询接⼝，订单号:{},月份:{}", chargeNo, month);
        try {
            if (StringUtils.isBlank(chargeNo)) {
                log.info("必填项存在空");
                return WebResponse.fail(ErrorCodeEnum.PARAM_ERROR);
            }
            OrderResponse response = consoleService.findOrder(chargeNo, month);
            log.info("订单查询接⼝响应为:{}", JSONObject.toJSONString(response));
            return WebResponse.success(response);
        } catch (BusinessException be) {
            throw be;
        } catch (Exception e) {
            log.info("订单查询处理异常", e);
            return WebResponse.fail(ErrorCodeEnum.SUPPLIER_FAIL);
        }
    }

    @PostMapping(value = "/productQuery")
    public WebResponse productQuery(@RequestBody ProductQueryRequest request) {
        log.info("维护信息查询接⼝，请求参数:{}", JSONObject.toJSONString(request));
        try {
            ProductQueryResponse response = mobileService.productQuery(request);
            log.info("维护信息查询接⼝响应为:{}", JSONObject.toJSONString(response));
            return WebResponse.success(response.getMaintainInfo());
        } catch (BusinessException be) {
            throw be;
        } catch (Exception e) {
            log.info("维护信息查询处理异常", e);
            return WebResponse.fail(ErrorCodeEnum.SUPPLIER_FAIL);
        }
    }

    @PostMapping(value = "/batchReorder")
    public WebResponse batchReorder(@RequestParam(value = "chargeNoList") List<String> chargeNoList,
                                    @RequestParam(value = "month") String month) {
        log.info("订单批量重发,商户订单号:{},交易年月:{}", JSONObject.toJSONString(chargeNoList), month);
        try {
            if (StringUtils.isBlank(month)) {
                log.info("交易年月不能为空");
                throw new BusinessException(ErrorCodeEnum.PARAM_ERROR);
            }
            boolean result = consoleService.batchReorder(chargeNoList, month);
            log.info("订单批量重发结果:{}", result);
            return WebResponse.success(result);
        } catch (BusinessException be) {
            throw be;
        } catch (Exception e) {
            log.info("订单批量重发处理异常", e);
            return WebResponse.fail(ErrorCodeEnum.SUPPLIER_FAIL);
        }
    }


    @PostMapping(value = "/staticsAmtQuery")
    public WebResponse staticsAmtQuery(@RequestParam(value = "month") String month) {
        log.info("日终金额统计信息查询接⼝，请求参数:{}", month);
        try {
            QueryWrapper<RechargeStatics> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("month", month);
            List<RechargeStatics> staticsList = rechargeStaticsService.list(queryWrapper);
            log.info("日终金额统计信息查询接⼝响应为:{}", JSONObject.toJSONString(staticsList));
            return WebResponse.success(staticsList);
        } catch (BusinessException be) {
            throw be;
        } catch (Exception e) {
            log.info("日终金额统计信息查处理异常", e);
            return WebResponse.fail(ErrorCodeEnum.SUPPLIER_FAIL);
        }
    }
}
